{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.min.css' %}">
    <style>
        .panel-default{
            margin-top: 10px;
        }

        {#让按钮向右偏移#}
        .panel-default .panel-heading{
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }
        .title-list{
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }
        .content{
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }
        .panel-body{
            padding: 0;
        }
        .title-list ul{
            padding-left: 15px;
        }
        .title-list ul a{
            display: block;
            padding: 5px 0;
        }
    </style>
{% endblock css %}

{% block content %}
<div class="container">
    <div class="panel panel-default">
          <div class="panel-heading">
              <div><i class="fa fa-book" aria-hidden="true"></i> wiki文档</div>
              <div class="function">
                  <a type="button" class="btn btn-success btn-xs" href="{% url 'web:manage:wiki_add' request.tracer.project.id %}"><i class="fa fa-plus-circle" aria-hidden="true"></i> 新建</a>
              </div>
          </div>
          <div class="panel-body">

              <div class="col-sm-3 title-list">

                  <ul id="catalog"></ul>

              </div>
              <div class="col-sm-9 content">
                <div class="col-sm-10 col-sm-offset-1">
                    <form class="form-horizontal" method="post">
                        {% csrf_token %}
                            {% for field in form %}
                                {% if field.name == 'content' %}
                                    <div class="form-group">
                                    <label  for="{{ field.id_for_label }}">{{ field.label }}</label>
                                        <div id="editor">
                                            {{ field }}
                                        </div>
                                        <span class="error-msg">{{ field.errors.0 }}</span>
                                    </div>
                                {% else %}
                                    <div class="form-group">
                                    <label  for="{{ field.id_for_label }}">{{ field.label }}</label>
                                        {{ field }}
                                        <span class="error-msg">{{ field.errors.0 }}</span>
                                    </div>
                                {% endif %}
                              {% endfor %}

                                <div class="form-group">
                                      <button type="submit" id="btnSubmit" class="btn btn-primary">提 交</button>
                                </div>
                        </form>
                </div>

              </div>

          </div>
        </div>
</div>
{% endblock content %}
{% block js %}

    <script src="{% static 'plugin/editor-md/editormd.js' %}"></script>
    <script type="text/javascript">
        var WIKI_DETAIL_URL = '{% url 'web:manage:wiki' request.tracer.project.id %}';
        var WIKI_UPLOAD_URL = '{% url 'web:manage:wiki_upload_img' request.tracer.project.id %}';

        $(function (){
            initCatalog();
            initEditorMd();

        });

        // Markdown编辑器的初始化
        function initEditorMd(){
            {#editor是对应编辑页的ID，path是指Markdown文件的依赖其他文件#}
            editormd('editor', {
                    placeholder: "请输入内容",
                    height: 400,
                    {#emoji: true,#}
                    path: "{% static 'plugin/editor-md/lib/' %}",

                    // 配置Markdown支持文件上传
                    imageUpload:true,
                    imageFormats:["jpg",'jpeg','png','gif'],
                    imageUploadURL:WIKI_UPLOAD_URL
            });

        }

        // 处理wiki目录
        function initCatalog(){
            $.ajax({
                url: '{% url 'web:manage:wiki_catalog' request.tracer.project.id %}',
                type: 'get',
                dataType: "json"
            }).done(function (data){
                if (data.code === 200){
                    $.each(data.msg, function (k, v){
                        var href = WIKI_DETAIL_URL + '?wiki_id=' + v.id;
                        var li = $('<li>').attr('id', 'id_'+v.id).append($('<a>').attr('href', href).text(v.title)).append($('<ul>'));
                        if (!v.parent_id){
                            // 没有父目录
                            $('#catalog').append(li);
                        }else{
                            // 有父目录
                            $('#id_'+v.parent_id).children('ul').append(li);
                        }
                    });
                }else{
                    alert("获取项目目录失败,请刷新");
                }
            }).fail(function (){
                console.log("网络超时");
            });
        }
    </script>
{% endblock js %}